package com.web.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Admin;
import com.common.utils.JwtUtil;
import com.web.main.entity.form.AdminModify;
import com.web.main.entity.param.AdminQuery;
import com.web.main.entity.form.AdminRegister;
import com.web.main.mapper.AdminMapper;
import com.web.main.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 长顺
 * @since 2020-11-21
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    //分页查询+条件
    @Override
    public void pageQuery(Page<Admin> page, AdminQuery adminQuery) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        if(adminQuery == null) {  //如果没条件，直接分页返回
            baseMapper.selectPage(page, queryWrapper);
            return;
        }

        String username = adminQuery.getUsername();  //用户名（电话）
        String name = adminQuery.getName();  //姓名
        String begin = adminQuery.getBegin();  //开始时间
        String end = adminQuery.getEnd();  //结束时间

        if(!StringUtils.isEmpty(username)) {
            queryWrapper.like("admin_username", username);
        }
        if(!StringUtils.isEmpty(name)) {
            queryWrapper.like("admin_name", name);
        }
        if(!StringUtils.isEmpty(begin)) {
            queryWrapper.gt("admin_create_time", begin);  //大于等于
        }
        if(!StringUtils.isEmpty(end)) {
            queryWrapper.lt("admin_create_time", end);  //小于等于
        }

        baseMapper.selectPage(page, queryWrapper);
    }

    //登录
    @Override
    public boolean login(String username, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_username", username);
        queryWrapper.eq("admin_password", password);
        Admin admin = baseMapper.selectOne(queryWrapper);

        return !StringUtils.isEmpty(admin);
    }

    //注册
    @Override
    public boolean register(AdminRegister voAdminRegister) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_username", voAdminRegister.getUsername());
        Admin selectOne = baseMapper.selectOne(queryWrapper);
        if(!StringUtils.isEmpty(selectOne)) {  //不为空,证明账号已经存在
            return false;
        }

        Admin admin = new Admin();
        admin.setAdminUsername(voAdminRegister.getUsername());
        admin.setAdminPassword(voAdminRegister.getPassword());
        admin.setAdminName(voAdminRegister.getName());

        return baseMapper.insert(admin) == 1;
    }

    //删除管理员
    @Override
    public boolean deleteAdmin(String adminId, String token) {
        //1.通过token获取用户名。
        String tokenUserName = JwtUtil.getUserName(token);
        //2.已经做了非控股判断。被操作id和登录人是同一个人，不能删本人。
        if(adminId.equals(tokenUserName)) {
            return false;
        }
        //3.获取操作者的级别
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_username", tokenUserName);  //操作者
        Admin admin = baseMapper.selectOne(queryWrapper);
        //4.被删除者级别必须低于操作者。
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("admin_id", adminId);  //被删除者id
        updateWrapper.lt("admin_role", admin.getAdminRole());  //被删除者账号小于操作者。

        return baseMapper.delete(updateWrapper) == 1 ;  //删除成功会返回1.
    }

    @Override
    public boolean modifyAdmin(AdminModify voAdminModify, String token) {
        if(voAdminModify == null) {
            return false;
        }

        String name = voAdminModify.getName();  //被修改成xx名字
        String password = voAdminModify.getPassword();  //被修改成xx密码
        Integer role = voAdminModify.getRole();  //被修改成xx等级。

        //先查询，再修改
        Admin admin = baseMapper.selectById(voAdminModify.getId());

        if(!StringUtils.isEmpty(name)) {
            admin.setAdminName(name);
        }

        if(!StringUtils.isEmpty(password)) {
            admin.setAdminPassword(password);
        }

        if(!StringUtils.isEmpty(role)) {
            //获取登录者账号
            String loginUserName = JwtUtil.getUserName(token);
            //1.获取操作者的级别
            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("admin_username", loginUserName);  //操作者
            Admin loginAdmin = baseMapper.selectOne(queryWrapper);
            //2.修改的级别只能比自己级别低。最低为0，最高为10
            Integer loginRole = loginAdmin.getAdminRole();  //获取登录这的等级
            if(loginRole <= role) {  //如果登录者的等级小于等于他修改的等级，就不能操作。
                return false;
            }
            //3.因为他看不到比他扽及高的，所以不用判断被修改者的等级是不是比他高
            admin.setAdminRole(role);  //修改等级
        }

        return baseMapper.updateById(admin) == 1;
    }

}
